package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> findCategoryByParentId(Long pid) {
        // 查询条件，mapper会把对象中的非空属性作为查询条件
        Category category = new Category();
        category.setParentId(pid);
        List<Category> list = categoryMapper.select(category);

        // 判断结果
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        // 使用自定义工具类，把Category集合转为DTO的集合
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }

    @Transactional
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    ///根据品牌查询商品类目
    public List<CategoryDTO> queryByBrandId(Long brandId) {
        List<Category> list = categoryMapper.queryByBrandId(brandId);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }

    //查询商品的方法,查询的都是CategoryDTO对象
    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {
        List<Category> categoryList = categoryMapper.selectByIdList(categoryIds);
        if (CollectionUtils.isEmpty(categoryList)) {
            // 没找到，返回404
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList,CategoryDTO.class);
    }


    //根据id的集合查询商品分类
    public List<CategoryDTO> queryByIds(List<Long> idList) {
        List<Category> categoryList = categoryMapper.selectByIdList(idList);
        if (CollectionUtils.isEmpty(categoryList)) {
            // 没找到，返回404
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
    }

    public List<CategoryDTO> queryAllByCid3(Long id) {
        Category c3 = categoryMapper.selectByPrimaryKey(id);
        if (c3 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        Category c2 = categoryMapper.selectByPrimaryKey(c3.getParentId());
        if (c2 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        Category c1 = categoryMapper.selectByPrimaryKey(c2.getParentId());
        if (c1 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        List<Category> categoryList = Arrays.asList(c1, c2, c3);
        return BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
    }
}
